﻿using System;
using System.Data;
using System.Windows.Forms;
using BLL;
using Model.Entity;

namespace LibrarySystem
{
    public partial class RenewForm : Form
    {
        RenewBLL rb = null;
        DataTable borrowTable = null;
        User user = null;
        String rfid = null;

        public RenewForm()
        {
            InitializeComponent();
            rb = new RenewBLL();
            borrowTable = new DataTable();
        }

        private void btn_search_Click(object sender, EventArgs e)
        {
            if (userText.Text == "")
            {
                MessageBox.Show("请输入用户编号！");
                user = null;
            }
            else
            {
                UserAndBorrow userAndBorrow = rb.getBorrowsByUser(int.Parse(userText.Text));
                if (userAndBorrow != null)
                {
                    user = userAndBorrow.user;
                    dgv_borrowDataGridView.DataSource = userAndBorrow.borrows;
                }
                else
                {
                    MessageBox.Show("用户不存在或没有借书！");
                    user = null;
                }
            }
        }

        private void btn_renewAll_Click(object sender, EventArgs e)
        {
            if (user != null)
            {
                int renewCount = rb.renewAllByUser(user);
                if (renewCount > 0)
                {
                    MessageBox.Show("已续借" + renewCount + "本书");
                    btn_search_Click(sender, e);
                }
                else
                {
                    MessageBox.Show("续借失败！续借次数太多或已过应还日期！");
                }
            }
            else
            {
                MessageBox.Show("请输入用户编号！");
            }
        }

        private void dgv_borrowDataGridView_SelectionChanged(object sender, EventArgs e)
        {
            if (dgv_borrowDataGridView.CurrentRow != null)
            {
                rfid = dgv_borrowDataGridView.CurrentRow.Cells["rfid"].Value.ToString();
            }
            else
            {
                rfid = null;
            }
        }

        private void btn_renew_Click(object sender, EventArgs e)
        {
            if (rfid != null)
            {
                if (rb.renewBook(rfid) != null)
                {
                    MessageBox.Show("续借成功！");
                    btn_search_Click(sender, e);
                }
                else
                {
                    MessageBox.Show("续借失败！续借次数太多或已过应还日期！");
                }
            }
            else
            {
                MessageBox.Show("请选择要续借的书！");
            }
        }
    }
}
